<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>第二题</title>
    <style>
        * {
            padding: 0px;
            margin: 0px;
            box-sizing: border-box;
        }

        .container {
            width: 100%;
            max-width: 1200px;
            margin: auto;
            display: flex;
            flex-direction: column;
        }

        .row {
            padding: 5px 0px;
            display: flex;
        }



        .row>.column {
            border: 1px solid #000;
            line-height: 30px;
        }

        .row>.column:nth-child(n + 2) {
            margin-left: 10px;

        }

        @media screen and (max-width: 600px) {
            .row:nth-child(1) {
                position: sticky;
                top: 0px;
                left: 0px;
                right: 0px;
                background-color: #fff;
            }
        }
    </style>
</head>

<body>
    <main class="container"></main>
</body>
<script>

    const container = document.querySelector(".container");



    function generPaper(layoutStr, jsonStr) {
        const obj = JSON.parse(jsonStr);
        const keys = Object.keys(obj);
        console.log("keys = ", keys);

        let beginIndex = 0;

        for (var i = 0; i < layoutStr.length; i++) {
            const v = layoutStr[i];
            if (v == '-') {
                createRow([1], keys.splice(0, 1).map(v => ({ key: v, value: obj[v] })));
            } else if (v == '[') {
                beginIndex = i;
            } else if (v == ']') {
                const bufferStr = layoutStr.substring(beginIndex + 1, i);
                console.log(bufferStr)
                const array = bufferStr.split("|");

                const values = keys.splice(0, array.length).map(v => ({ key: v, value: obj[v] }));
                console.log("values = ", values);
                createRow(array, values);
            }
        }

        function createRow(arr, values) {
            const div = document.createElement("div");
            div.classList.add("row");
            for (var i in arr) {
                const column = document.createElement("div");
                // 弹性盒子分配剩余空间
                column.style.flexGrow = arr[i];
                column.classList.add("column");
                column.innerText = values[i].key + ': ' + values[i].value;
                div.appendChild(column);
            }
            container.appendChild(div);
        }
    }

    const layoutStr = '-[1|1|1][2|1]-';
    const jsonStr = `{
        "姓名": "刘强",
        "年龄": 35,
        "学校": "四川工商职业学院",
        "专业": "软件工程",
        "居住城市": "成都市",
        "工作年限": "1年",
        "求职意向": "WEB工程师"
    }`
    generPaper(layoutStr, jsonStr);


</script>

</html>